
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class CustomScrollTest extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _CustomScrollState();
  }
}

class _CustomScrollState extends State<CustomScrollTest> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Material(
      child:CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            pinned: false,
            expandedHeight: 250.0,
            flexibleSpace: FlexibleSpaceBar(
              title: Text('测试测试'),
              background: Image.asset('bizhi.jpg', fit: BoxFit.cover,),
            ),
          ),
          SliverPadding(
            padding: EdgeInsets.all(10),
            sliver: SliverGrid(
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2,
                childAspectRatio: 4.0,
              ),
              delegate: SliverChildBuilderDelegate(
                  (context, index) {
                    return Container(
                      alignment: Alignment.center,
                      color: Colors.orange[100 * (index % 9)],
                      child: Text('grid item $index'),
                    );
                  },
                childCount: 20
              ),
            ),
          ),
          SliverFixedExtentList(
            itemExtent: 50.0,
            delegate: SliverChildBuilderDelegate(
                (context,index) {
                  return Container(
                    alignment: Alignment.center,
                    color: Colors.green[100 * (index % 9)],
                    child: Text('text item $index'),
                  );
                },
              childCount: 20
            ),
          ),
        ],
      ),
    );
  }
}

class ScrollControllerTest extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ScrollControllerState();
  }
}

class _ScrollControllerState extends State<ScrollControllerTest> {
  ScrollController _scrollController = ScrollController();
  bool _isShowBack = false;
  var _progress = "0%";
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _scrollController.addListener(() {
      if (_scrollController.offset > 50 * 20) {
        setState(() {
          _isShowBack = true;
        });
      } else {
        setState(() {
          _isShowBack = false;
        });
      }
    });
  }
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _scrollController.dispose();
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('list控制'),
      ),
      body:
          NotificationListener<ScrollNotification>(
            onNotification: (ScrollNotification scroll) {
              double progress = scroll.metrics.pixels / scroll.metrics.maxScrollExtent;
              setState(() {
                _progress = "${(progress * 100).toInt()} % ";
              });
              return false;
            },
            child: Stack(
              alignment: Alignment.center,
              children: <Widget>[
                Scrollbar(
                  child: ListView.builder(
                    controller: _scrollController,
                    itemCount: 100,
                    itemExtent: 50,
                    itemBuilder: (context, index) => Container(
                      alignment: Alignment.center,
                      color: Colors.green[100 * (index % 9)],
                      child: Text('text item $index'),
                    ),
                  ),
                ),
                CircleAvatar(
                  radius: 30,
                  child: Text(_progress),
                  backgroundColor: Colors.deepOrange,
                ),
              ],
            ),
          ),
      floatingActionButton: _isShowBack? FloatingActionButton(
        child: Icon(Icons.vertical_align_top),
        backgroundColor: Colors.lightGreen,
        onPressed: () {
          _scrollController.animateTo(.0, duration: Duration(microseconds: 5000), curve: Curves.ease);
        },
      ) : null,
    );
  }
}